package com.heatup.apt.api.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.heatup.apt.api.service.CoachCourseFeeService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.heatup.apt.common.dao.CoachCourseFeeMapper;
import com.heatup.apt.common.dao.CoachSigninLogMapper;
import com.heatup.apt.common.dao.CourseSignMapper;
import com.heatup.apt.common.dao.coach.CoachInfoMapper;
import com.heatup.apt.model.coach.CampCountFeeDetail;
import com.heatup.apt.model.coach.CoachCourseFee;
import com.heatup.apt.model.coach.CoachCourseFeeVO;
import com.heatup.apt.model.coach.CoachInfo;
import com.heatup.apt.model.course.CoursePrivteFeeVO;

/**
 * CoachCourseFeeVO 表数据服务层接口实现类
 */
@Service
public class CoachCourseFeeServiceImpl extends SuperServiceImpl<CoachCourseFeeMapper, CoachCourseFee> implements CoachCourseFeeService {

    public static final Logger log = LoggerFactory.getLogger(CoachCourseFeeServiceImpl.class);
    @Autowired
    private CoachInfoMapper coachInfoMapper;
    @Autowired
    private CoachCourseFeeMapper coachCourseFeeMapper;
    @Autowired
    private CourseSignMapper courseSignMapper;
    @Autowired
    private CoachSigninLogMapper coachSigninLogMapper;


    @Override
    public List<CoursePrivteFeeVO> queryCoachPrivateFeeDetail(Map<String, Object> paramsMap) {
    	return courseSignMapper.queryPrivateCoachFee(paramsMap);
    }

    @Override
    public Map<String, Object> queryCoachCampFeeDetail(Map<String, Object> paramsMap) {
        Map<String, Object> map = new HashMap<>();
        List<CampCountFeeDetail> list = coachSigninLogMapper.queryCampCoachFee(paramsMap);
        int totalCount = coachSigninLogMapper.queryCampCoachCount(paramsMap);
        map.put("campList", list);
        map.put("campCount", totalCount);
        return map;
    }


    /**
     * 根据教练 ID 查询教练课时费详情
     *
     * @param coachId 教练 ID
     * @return 返回教练课时费详情
     */
    @Override
    public CoachCourseFee queryByCoachId(Integer coachId) {
        CoachCourseFee coachCourseFeeQuery = new CoachCourseFee();
        coachCourseFeeQuery.setCoachId(coachId);
        return coachCourseFeeMapper.selectOne(coachCourseFeeQuery);
    }

    /**
     * 教练课时费分页
     *
     * @param coachCourseFee 教练
     * @return 教练课时费分页
     */
    @Override
    public JSONObject page(CoachCourseFeeVO coachCourseFee) {
        if (coachCourseFee.getCoachId() == null || coachCourseFee.getPageIndex() == null || coachCourseFee.getPageSize() == null) {
            return null;
        }
        if (coachCourseFee.getPageIndex() > 0) {
            coachCourseFee.setPageIndex(coachCourseFee.getPageIndex() - 1);
            coachCourseFee.setPageIndex(coachCourseFee.getPageIndex() * coachCourseFee.getPageSize());
        }
        JSONObject resultJson = new JSONObject();
        List<CoachCourseFeeVO> courseFeeVOList = coachCourseFeeMapper.pageList(coachCourseFee);
        resultJson.put("list", courseFeeVOList);
        resultJson.put("count", coachCourseFeeMapper.pageCount(coachCourseFee));

        resultJson.put("totalTotalSum", this.queryCoachFeeTotalSum(coachCourseFee));
        return resultJson;
    }

    /**
     * 查询教练所有课时费总计
     *
     * @param courseFeeVO 教练 ID 年/月 参数
     * @return 返回教练所有课时费总计
     */
    @Override
    public BigDecimal queryCoachFeeTotalSum(CoachCourseFeeVO courseFeeVO) {

        if (courseFeeVO == null || courseFeeVO.getCoachId() == null) return new BigDecimal(0);
        courseFeeVO.setPageIndex(null);
        courseFeeVO.setPageSize(null);
        List<CoachCourseFeeVO> courseFeeVOList = coachCourseFeeMapper.pageList(courseFeeVO);
        BigDecimal totalTotalSum = new BigDecimal(0);
        for (CoachCourseFeeVO feeVO : courseFeeVOList) {
            totalTotalSum = totalTotalSum.add(feeVO.getTotalSum());
        }
        return totalTotalSum;
    }

    /**
     * 查询教练按年月度汇总课时费
     *
     * @param mobile 教练手机号
     * @param year   年
     * @return 教练按年月度汇总课时费
     */
    @Override
    public JSONObject queryCoachFeeMonth(String mobile, Integer year) {
        if (StringUtils.isBlank(mobile) || year == null) return null;

        CoachInfo coachInfo = coachInfoMapper.queryCoachByMobile(mobile);
        JSONObject resultJson = new JSONObject();
        if (coachInfo != null) {

            CoachCourseFeeVO coachCourseFeeVO = new CoachCourseFeeVO();
            coachCourseFeeVO.setCoachId(coachInfo.getId());
            coachCourseFeeVO.setYear(year);

            List<CoachCourseFeeVO> courseFeeVOList = coachCourseFeeMapper.pageList(coachCourseFeeVO);
            Integer courseCount = 0;
            for (CoachCourseFeeVO feeVO : courseFeeVOList) {
                courseCount += feeVO.getTeamCourseCount();
                courseCount += feeVO.getPrivateCourseCount();
                courseCount += feeVO.getCampCourseCount();
            }
            resultJson.put("courseCount", courseCount);

            for (CoachCourseFeeVO feeVO : courseFeeVOList) {
                feeVO.setAdjustReduceFee(null);
                feeVO.setAdjustSupplementFee(null);
                feeVO.setAdjustReduceFeeRemark(null);
                feeVO.setAdjustSupplementFeeRemark(null);
                feeVO.setTeamCourseCount(null);
                feeVO.setPrivateCourseCount(null);
                feeVO.setCampCourseCount(null);
                feeVO.setTeamCourseFeeSum(null);
                feeVO.setPrivateCourseFeeSum(null);
                feeVO.setCampCourseFeeSum(null);
                feeVO.setYear(null);
            }
            resultJson.put("list", courseFeeVOList);
            resultJson.put("count", coachCourseFeeMapper.pageCount(coachCourseFeeVO));
            resultJson.put("year", year);
            resultJson.put("totalTotalSum", this.queryCoachFeeTotalSum(coachCourseFeeVO));
        }
        return resultJson;

    }

    /**
     * 查询教练月度补贴/扣费
     *
     * @param year  年
     * @param month 月
     * @return 教练月度补贴/扣费
     */
    @Override
    public JSONObject queryCoachAdjustFeeMonth(String mobile, Integer year, Integer month) {

        if (StringUtils.isBlank(mobile) || year == null || month == null) return null;

        CoachInfo coachInfo = coachInfoMapper.queryCoachByMobile(mobile);
        JSONObject resultJson = new JSONObject();
        if (coachInfo != null) {

            CoachCourseFeeVO coachCourseFeeVO = new CoachCourseFeeVO();
            coachCourseFeeVO.setCoachId(coachInfo.getId());
            coachCourseFeeVO.setYear(year);
            coachCourseFeeVO.setMonth(month);

            List<CoachCourseFeeVO> courseFeeVOList = coachCourseFeeMapper.pageList(coachCourseFeeVO);
            if (courseFeeVOList != null && courseFeeVOList.size() > 0) {
                resultJson.put("supplementFee", courseFeeVOList.get(0).getAdjustSupplementFee());
                resultJson.put("supplementFeeRemark", courseFeeVOList.get(0).getAdjustSupplementFeeRemark());
                resultJson.put("reduceFee", courseFeeVOList.get(0).getAdjustReduceFee());
                resultJson.put("reduceFeeRemark", courseFeeVOList.get(0).getAdjustReduceFeeRemark());
                resultJson.put("totalFee", courseFeeVOList.get(0).getAdjustSupplementFee().subtract(courseFeeVOList.get(0).getAdjustReduceFee()));
            }
        }
        return resultJson;
    }
}